package com.example.springboot.controller;

/*
 * 功能：controller层是和前端交互的窗口，前端调用controller
 * 日期：2024-3-22 22:23
 * 作者：RabbitQ
 */

import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin //跨域访问 前端访问数据
@RestController //表示下面的类可以接收HTTP请求
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    //新增用户信息
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.insertUser(user);
        return Result.success();
    }

    //更新用户信息，根据id进行修改
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    //删除用户信息，根据id单个进行删除，删除一般要在路径上指定信息——例如id
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }

    //删除-批量删除
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> id_all) {
        userService.batchDelete(id_all);
        return Result.success();
    }

    @CrossOrigin //跨域访问 前端访问数据
    //查询-全部用户信息
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> user = userService.selectAll();
        return Result.success(user);
    }

    //查询——条件查询
    @GetMapping("/select_By_name_username")
    public Result select_By_name_username(@RequestParam String username,
                                          @RequestParam String name) {
        List<User> user = userService.select_By_name_username(username, name);

        return Result.success(user);
    }

    //通过用户名查找用户
    @GetMapping("/selectByUsername")
    public Result selectByUsername(@RequestBody User user) {
        user = userService.selectByUsername(user.getUsername());

        return Result.success(user);
    }


    //通过用户名查找用户
    @GetMapping("/selectByUsernameTwo")
    public Result selectByUsername(@RequestParam String username) {

        User user = new User();
        user = userService.selectByUsername(username);

        return Result.success(user);
    }



    //通过用户名和角色查询
    @GetMapping("/selectByUsernameRole")
    public Result selectByUsernameRole(@RequestParam  String username,
                                       @RequestParam  String role) {
        List<User> userList = new ArrayList<>();

        userList = userService.selectByUsernameRole(username, role);

        return Result.success(userList);
    }


    @PostMapping("/delByUserInfo")
    public Result delByUserInfo(@RequestBody User userOne){
        //通过id删除用户信息
        userService.deleteUser(userOne.getId());

        List<User> userList = new ArrayList<>();
        userList = userService.selectAll();
        return Result.success(userList);
    }

    @PostMapping("/updateByUserInfo")
    public Result updateByUserInfo(@RequestBody User userOne){
        //通过用户id更新用户角色
        userService.updateUserByID(userOne);

        List<User> userList = new ArrayList<>();
        userList = userService.selectAll();
        return Result.success(userList);
    }

    //个人信息修改界面——通过用户名称修改个人信息
    @PostMapping("/updateUserByUsername")
    public Result updateUserByUsername(@RequestBody User user){

        userService.updateUsername(user);

        //更新user信息
        user = userService.selectByUsername(user.getUsername());

        return Result.success(user);
    }



}
